﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMPLib;

namespace CD_Reader
{
    public class CD_Reader
    {
        #region Fields (5) 

        IWMPCdrom cd;
        WMPLib.WindowsMediaPlayer player;
        IWMPCdromRip rip ;
        #endregion Fields 

        #region Constructors (2) 

        public CD_Reader(string inDriveLetter):this()
        {
            cd = player.cdromCollection.getByDriveSpecifier(inDriveLetter);
        }

        public CD_Reader()
        {
            player = new WindowsMediaPlayer();
        }

        #endregion Constructors 

        #region Methods (11) 

        // Public Methods (11) 

        public bool isCD_Ready()
        {
           return  ! cd.Playlist.name.Equals("No disc in drive");
        }

        public string GetArtist()
        {
            if (this.cd.Playlist.count > 0)
                return cd.Playlist.Item[0].getItemInfo("Artist");
            else
                return string.Empty;
        }

        public string GetCD_Title()
        {
            return cd.Playlist.name;
        }

        public string GetRipProgress()
        {
            return rip.ripProgress.ToString();
        }

        public string GetRipState()
        {
            string returnValue = string.Empty;
          
            switch (rip.ripState)
            {
                case WMPRipState.wmprsRipping:
                    returnValue = "Still Ripping";
                    break;
                case WMPRipState.wmprsStopped:
                    returnValue = "Done";
                    break;
                case WMPRipState.wmprsUnknown:
                    returnValue = "Unknown";
                    break;

            }
           return returnValue;
        }

        public List<string> GetTracks()
        {
           List<string> CurrentTracks = new List<string>();
            for (int i = 0; i < cd.Playlist.count; i++)
            {
                string trackName = cd.Playlist.Item[i].name;
                CurrentTracks.Add(trackName);
            }
            return CurrentTracks;
        }

        public List<string> ListAllAttributes()
        {
            List<string> myAttributes = new List<string>();
            IWMPMedia song = cd.Playlist.Item[0];
            
            int numAttributes = song.attributeCount;
            for (int i = 0; i < numAttributes; i++)
            {
                string s = song.getAttributeName(i) + "  |  " + song.getItemInfo(song.getAttributeName(i));
                myAttributes.Add(s);
            }

            myAttributes.Add("Duration | " + song.durationString);
            return myAttributes;
        }

        public void RipAllTracks()
        {
            //d consider moving this intialization (constructor?) otherwise 
            //d if someone implements RipSingleTrack they might not realize they need to set 'rip'
           rip = (IWMPCdromRip)cd;
            rip.startRip();
           
        }

        public void SetIndividualTrackToRip(IWMPMedia inTrack)
        {
            inTrack.setItemInfo("SelectedForRip", "True");
           
        }

        public void SetNewDriveLetter(string inNewDriveLetter)
        {
            cd = player.cdromCollection.getByDriveSpecifier(inNewDriveLetter);
        }
       
        public void SetTracksToRip(List<string> inTracksToRip)
        {

            for (int i = 0; i < cd.Playlist.count; i++)
            {
                if (inTracksToRip.Contains(cd.Playlist.Item[i].name))
                {
                    cd.Playlist.Item[i].setItemInfo("SelectedForRip", "True");
                }
                else
                {
                    cd.Playlist.Item[i].setItemInfo("SelectedForRip", "False");
                }
            }

        }

        #endregion Methods 
    }
}
